# -*- coding: UTF-8 -*-
import datetime
import os

import serial
import time
import threading
from datetime import datetime

from utils.PathUtil import BASE_PATH
from utils.time_utils import get_datetime


class SerialUtil:

    def __init__(self, PORTX, BPS):
        self.PORTX = PORTX
        self.BPS = BPS
        self.com = serial.Serial(self.PORTX, self.BPS)
        self.thread_read = threading.Thread(target=self.readMsg)
        self.thread_read.start()

    def readMsg(self):
        pass

    def sendMsg(self, cmd):
        try:
            cmd = cmd + '\r'
            cmd = cmd.encode("utf-8")  # gbk
            self.com.write(cmd)
            time.sleep(0.1)
        except:
            print('指令发送失败:', cmd)

    # 继电器
    def sendMsg2(self, cmd):
        try:
            self.com.write(cmd)
            time.sleep(0.1)
        except:
            print('指令发送失败:', cmd)
